package com.baizhi.service.impl;

import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    //管理员登录页面
    @Override
    public boolean adminLogin(String username, String password) {
        Admin admin = adminDao.selectAdminByName(username);
        if(admin!=null && password.equals(admin.getPassword())){
            return true;
        }else{
            return false;
        }
    }

    //根据管理员姓名查询管理员对象
    @Override
    public Admin selectAdminByName(String username) {
        return adminDao.selectAdminByName(username);
    }

    //查询所有管理员  分页+条件
    @Override
    public Map listAdmin(int page, int limit, String name) {
        int start=(page-1)*limit;
        List<Admin> listAdmin = adminDao.listAdmin(start, limit, name);
        int count = adminDao.countAdmin(name);
        Map map=new HashMap();
        map.put("data",listAdmin);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    //添加管理员
    @Override
    public Map insertAdmin(Admin admin) {
        int i = adminDao.insertAdmin(admin);
        Map map=new HashMap();
        if(i>0){
            map.put("insertAdmin","添加成功！");
        }else{
            map.put("insertAdmin","添加失败！");
        }
        return map;
    }

    //查询管理员对象
    @Override
    public Admin selectAdminById(Integer id) {
        return adminDao.selectAdminById(id);
    }

    //修改管理员
    @Override
    public Map updateAdmin(Admin admin) {
        int i = adminDao.updateAdmin(admin);
        Map map=new HashMap();
        if(i>0){
            map.put("updateAdmin","修改成功！");
        }else{
            map.put("updateAdmin","修改失败！");
        }
        return map;
    }

    //批量删除管理员
    @Override
    public Map multiDeleteAdmin(Integer[] ids) {
        int i = adminDao.multiDeleteAdmin(ids);
        Map map=new HashMap();
        if(i>0){
            map.put("multiDeleteAdmin","删除成功！");
        }else{
            map.put("multiDeleteAdmin","删除失败！");
        }
        return map;
    }
}
